-- ====================================
-- Initialize any useful constants
-- ====================================
-- local INSPECTION_FLAG = 1 -- used in CheckInteractDistance http://www.wowwiki.com/API_CheckInteractDistance
Orunj.INSPECT_TALENT_EVENT = "INSPECT_TALENT_READY"

local ZONE_EVENTS = {
	"PLAYER_ENTERING_WORLD", 
	"ZONE_CHANGED", 
	"ZONE_CHANGED_NEW_AREA", 
	"ZONE_CHANGED_INDOORS",
	"WORLD_MAP_UPDATE",
}
-- Check if the event is a zone-related event.
local function IsZoneEvent(event) 
	for index, event in ipairs(ZONE_EVENTS) do 
		if event == ZONE_EVENTS[index] then 
			return true 
		end
	end 
	return false 
end

Orunj.COMBAT_LOG_EVENT = "COMBAT_LOG_EVENT_UNFILTERED"
Orunj.ADDON_LOADED_EVENT = "ADDON_LOADED"


-- ====================================
-- Driver Functions and Event Handlers
-- ====================================
-- Function to add slash commands [http://www.wowwiki.com/SlashCmdList_AddSlashCommand]
local function CreateSlashCommand(name, func, ...)
    SlashCmdList[name] = func
    local command = ''
    for i = 1, select('#', ...) do
        command = select(i, ...)
        if strsub(command, 1, 1) ~= '/' then
            command = '/' .. command
        end
        _G['SLASH_'..name..i] = command
    end
end

-- Simple HelloWorld function. Invoked when you type in-game: /hai
local function HelloWorld()
	print(Orunj.cmdprefix .. "ohai!")
end

-- Command line handler. Invoked when you type in-game: /orunj <arg>
-- <arg> is optional. 
-- When arg is absent, or when it is "toggle", then recording will start/stop.
-- When arg is "clear", then OrunjSave will be cleared.
-- When arg is "view", the most recent snapshot will be displayed. [TODO]
local function OrunjCommandLineHandler(msg, editbox)
    if msg == "toggle" or msg == "" then
		Orunj:ToggleRecording()
	elseif msg == "clear" or msg == "clean" then
		Orunj:ClearSaved()
	elseif msg == "view" then
		-- print the latest snapshot.
	end
end

-- Per-frame update function [Orunj.frame:SetScript("OnUpdate", OnUpdate)]
local function OrunjOnUpdate()
	-- Ensure we are recording.
	if Orunj.recording then
		-- Inspect the next available player, if need be.
		if Orunj.inspectEnabled then
			Orunj:DoInspect()
		end
		
		-- Check if we need to manually update the map info in the timeline..
		Orunj:CheckForceMapUpdate() -- OrunjTimeline.lua
		
		-- Update the timeline. (See OrunjTimeline.lua)
		Orunj:UpdateTimeline() -- OrunjTimeline.lua
	end
	
	-- Update icon positions.
	--[[ DISABLED FOR NOW.
	Orunj:UpdateIcons() -- OrunjGUI.lua
	]]--
end

-- Orunj event handler.
local function OrunjOnEvent(this, event, arg1)
	if event == Orunj.ADDON_LOADED_EVENT then
		--[[ 
		According to [http://www.wowwiki.com/Saving_variables_between_game_sessions#Common_Pitfalls],
		Saved variables are loaded after the addon code is executed.
		As such, we check for the "ADDON_LOADED" event after all the
		AddOn code is executed.
		]]--
		Orunj:LoadSettings()
	end

	-- Stuff to handle while recording
	if Orunj.recording then
		if event == Orunj.INSPECT_TALENT_EVENT then
			-- Record the inspected player's talents.
			Orunj:InspectEventFired() -- OrunjInspect.lua
		elseif event == Orunj.COMBAT_LOG_EVENT then
			-- Handle the Combat log event.
		elseif IsZoneEvent(event) then
			-- Record which zone we are in now.
			Orunj:UpdateGlobalTimelineMap() -- OrunjTimeline.lua
		end
	end
end

-- ====================================
-- Register the slash commands
-- ====================================
CreateSlashCommand("HAI", HelloWorld, "hai")
-- Create the slash command: "/orunj <arg>"
CreateSlashCommand("ORUNJ", OrunjCommandLineHandler, "orunj")

-- ====================================
-- Register the events and handlers
-- ====================================
-- Zone change events.
for index, event in ipairs(ZONE_EVENTS) do
	Orunj.frame:RegisterEvent(ZONE_EVENTS[index])
end
-- Combat log event.
Orunj.frame:RegisterEvent(Orunj.COMBAT_LOG_EVENT)
Orunj.frame:RegisterEvent(Orunj.ADDON_LOADED_EVENT)
-- Scripts.
Orunj.frame:SetScript("OnUpdate", OrunjOnUpdate)
Orunj.frame:SetScript("OnEvent", OrunjOnEvent)